import config from "config"
const formatTime = date => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':');
};

const formatNumber = n => {
  n = n.toString();
  return n[1] ? n : '0' + n;
}; 

//弹出alert框    title标题  content内容   fun点击确定的回调函数
var alert1 = function(content = "内容", title = "提示", fun) {
  if (typeof title == "number" || title == "") {
    title = "温馨提示";
  }
  if (typeof content == "number" || content == "") {
    content = "请填写提示内容";
  }
  wx.showModal({
    title: title,
    content: content,
    showCancel: false,
    success(res) {
      if (res.confirm) {
        typeof fun == "function" && fun();
      }
    }
  })
}

//弹出对话框  title标题   content内容   fun点击确定的回调   fun1点击取消的回调
var confirm1 = function(content, title, fun, fun1) {
  if (typeof title == "number" || title == "") {
    title = "提示";
  }
  if (typeof content == "number" || content == "") {
    content = "请填写提示内容";
  }
  wx.showModal({
    title: title,
    content: content,
    success(res) {
      if (res.confirm) {
        typeof fun == "function" && fun();
      } else {
        typeof fun1 == "function" && fun1();
      }
    }
  })
}


//成功提示  txt提示文字     time提示时间
var showSuccess1 = function(txt = "提示", time = 2000) {
  wx.showToast({
    title: txt,
    icon: "success",
    duration: time,
  })
}


//文字提示  txt提示文字     time提示时间
var showText1 = function(txt = "提示", time = 2000, fun) {
  wx.showToast({
    title: txt,
    icon: "none",
    duration: time,
    success(res) {
      typeof fun == "function" && fun(res)
    }
  })
}

//显示loading
var showLoading1 = function(txt) {
  txt = txt || "加载中...";
  wx.showLoading({
    title: txt,
  })
}

// ajax请求
var ajax1 = function(isurl, data, fun, fun1) {
  let isArr = false;
  isArr = Array.isArray(data);
  let flag = "";
  if (isArr) {
    flag = 1
  } else {
    flag = data.header ? 1 : "";
    delete data.header;
  }
  wx.request({
    url: isurl,
    data: data,
    header: {
      'content-type': flag ? "application/x-www-form-urlencoded" : 'application/json'
    },
    method: "POST",
    success(res) {
      
      typeof fun == "function" && fun(res)
    },
    fail(res1) {
      
      typeof fun1 == "function" && fun1(res1)
    }
  })
}

// async ajax请求
const wxRequest = async(url, params = {}) => {
  Object.assign(params, {
    token: wx.getStorageSync('token')
  })
  // 所有的请求，header默认携带token
  let header = params.header || {
    'Content-Type': 'application/json',
    'token': params.token || ''
  }
  let data = params.data || {}
  let method = params.method || 'GET'
  // hideLoading可以控制是否显示加载状态
  if (!params.hideLoading) {
    wx.showLoading({
      title: '加载中...',
    })
  }
  let res = await new Promise((resolve, reject) => {
    wx.request({
      url: url,
      method: method,
      data: data,
      header: header,
      success: (res) => {
        if (res && res.statusCode == 200) {
          resolve(res.data)
        } else {
          reject(res)
        }
      },
      fail: (err) => {
        reject(err)
      },
      complete: (e) => {
        wx.hideLoading()
      }
    })
  })
  return res
}


module.exports = {
  formatTime,
  alert: alert1, //alert弹窗
  confirm: confirm1, //对话框
  showSuccess: showSuccess1, //成功提示
  showText: showText1, //文字提示
  showLoading: showLoading1, //加载提示
  ajax: ajax1, //ajax请求
  wxRequest: wxRequest, // 异步调用Request
}